package by.alex.dzeshko.facebooksuperclient;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import by.alex.dzeshko.facebooksuperclient.database.DbManager;
import by.alex.dzeshko.facebooksuperclient.oauth.FBOAuthHelper;
import by.alex.dzeshko.facebooksuperclient.utils.InetChecker;

public class FeedMainActivity extends FragmentActivity implements OnPageChangeListener {
	
	private static final int IND_SELECTED = R.drawable.shape_round_selected, IND_UNSELECTED = R.drawable.shape_round_unselected;

	private static final int FRAGMENTS_COUNT = 3;

	private int mCurentPage = 1;
	
	private ImageView mIndicatorNews, mIndicatorProfile, mIndicatorFriends; 
	
	private ViewPager mViewPager;

	private DbManager mDbHelper;
	
	private String mUserId;
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_feed_main, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == R.id.menu_sign_out){
			if(!InetChecker.checkInetConnection(this))
				return false;	
			if (FBOAuthHelper.isLogged(this)) {
				FBOAuthHelper.exit(this);
			}
			try{
				mDbHelper = new DbManager(this, null);
				mDbHelper.dropDatabase(mDbHelper.getWritableDatabase());
				mDbHelper.close();
			}
			catch(Exception e){
				Log.e("onSignClick",e.toString());
			}
			Intent intent = new Intent(this, AuthActivity.class);
			startActivity(intent);
			finish();
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		SharedPreferences prefs = getSharedPreferences(FBOAuthHelper.AUTH_PREFS, MODE_PRIVATE);
		mUserId = prefs.getString(FBOAuthHelper.USER_ID, null);
		if (!FBOAuthHelper.isLogged(this)) {
			startActivity(new Intent(this,AuthActivity.class));
			finish();
		}
		mIndicatorNews = (ImageView) findViewById(R.id.indicator_news);
		mIndicatorProfile = (ImageView) findViewById(R.id.indicator_profile);
		mIndicatorFriends = (ImageView) findViewById(R.id.indicator_friends);
		initializeFragments();
	}
	
	private void initializeFragments() {
		mViewPager = (ViewPager) findViewById(R.id.vpListContainer);
		mViewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
			
			@Override
			public int getCount() {
				return FRAGMENTS_COUNT;
			}
			
			@Override
			public Fragment getItem(int position) {
				switch (position) {
				case 0:
					return new ProfilePageFragment();
				case 1:
					return new ListNewsFragment();		
				case 2:
					return new ListFriendsFragment();	
				}
				return null;
			}
			
		});
		mViewPager.setOnPageChangeListener(this);
		mViewPager.setCurrentItem(mCurentPage);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if(requestCode==2){
			if(resultCode==RESULT_CANCELED)
				return;
			if(resultCode==RESULT_OK){
				//
			}
		}
		else return;
	}

	@Override
	public void onPageSelected(int position) {
		//mCurentPage=position;
	}

	@Override
	public void onPageScrollStateChanged(int arg0) { }

	@Override
	public void onPageScrolled(int position, float arg1, int arg2) { 
		mCurentPage = position;
		switch (position) {
		case 0: 
			{mIndicatorFriends.setImageResource(IND_UNSELECTED);
			mIndicatorNews.setImageResource(IND_UNSELECTED);
			mIndicatorProfile.setImageResource(IND_SELECTED);
			break;}
		case 1: 
			{mIndicatorFriends.setImageResource(IND_UNSELECTED);
			mIndicatorNews.setImageResource(IND_SELECTED);
			mIndicatorProfile.setImageResource(IND_UNSELECTED);
			break;}
		case 2: 
			{mIndicatorFriends.setImageResource(IND_SELECTED);
			mIndicatorNews.setImageResource(IND_UNSELECTED);
			mIndicatorProfile.setImageResource(IND_UNSELECTED);
			break;}
		default: 
			break;
		}
	}

}
